package com.bgba.oer.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.AssetSummary;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Contact;
import com.flashline.registry.openapi.entity.RelationshipType;
import com.flashline.registry.openapi.query.AssetCriteria;
import com.flashline.registry.openapi.query.ContactCriteria;
import com.flashline.registry.openapi.query.SearchTerm;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;

public class ContactService {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("======================== ");
		String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
		String username = " L0269239";
		String password = "finde2011";
		/*
		String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
		String username = " admin";
		String password = "admin123";
		*/
		
		ContactService instance = new ContactService();
		//instance.addRelationships(urlConnectionLogin, username, password);
		
		try {		///////////////////////////////////////////////////////////
		// Connect to Oracle Enterprise Repository
		///////////////////////////////////////////////////////////
		URL lURL = null;
		lURL = new URL(urlConnectionLogin);
		FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

		///////////////////////////////////
		// Login to OER
		///////////////////////////////////
		AuthToken authToken = repository.authTokenCreate(username, password);
		
		Contact contact = instance.searchContactByName(repository, authToken, "Tiberti, Alberto");
		System.out.println("=========== contact.getID="+contact.getID());
		System.out.println("=========== contact.Name="+contact.getName());
		System.out.println("=========== contact.Email="+contact.getEmail());
		System.out.println("=========== contact.PhoneNumber="+contact.getPhoneNumber());

		/*
		Contact newContact = new Contact();
		newContact.setName("SILVA, OSCAR FERNANDO");
		newContact.setEmail("fernando.o.silva@bancogalicia.com.ar");
		newContact.setPhoneNumber("5382");
		
		Contact pContact = instance.addContact(repository, authToken, newContact);
		
		System.out.println("=========== pContact.getID"+pContact.getID());
		System.out.println("=========== pContact.Name()"+pContact.getName());
		System.out.println("=========== pContact.Email()"+pContact.getEmail());
		System.out.println("=========== pContact.PhoneNumber()"+pContact.getPhoneNumber());
		*/
		
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}

	
	public static Contact searchContactByName(FlashlineRegistry repository, AuthToken authToken, String contactNameCriteria) throws RemoteException, OpenAPIException {
		System.out.println("\n ######################################################");			

		System.out.println("======================================= Searching conctat with fullName="+contactNameCriteria);

		ContactCriteria contactCriteria = new ContactCriteria();
		contactCriteria.setNameCriteria(contactNameCriteria);
		//contactCriteria.set
		Contact[] contacts = repository.contactQuery(authToken, contactCriteria);
		Contact contact = null;

		if (contacts != null && contacts.length == 1){
			contact = contacts[0];
			System.out.println("======================================= FOUND conctat with ID="+contact.getID());
			System.out.println("======================================= FOUND conctat with Name="+contact.getName());
		} else if (contacts != null  && contacts.length > 1){
			System.out.println("======================================= WARM!!! FOUND  duplicated conctat with name="+contactNameCriteria + ", contact resultSet="+contacts.length);
			String[] messageArgs = {"Error, Contact duplicated"};
			throw new OpenAPIException(messageArgs, 1, "", "", "ERROR" );

		} else if (contacts == null || contacts.length == 0){
			System.out.println("======================================= NOT FOUND conctat with name="+contactNameCriteria + ", contact resultSet=0");
		}			

		return contact;
	}

	public static Contact addContact(FlashlineRegistry repository, AuthToken authToken, Contact newContact) throws RemoteException, OpenAPIException {
		System.out.println("\n ######################################################");			
			
		Contact persistentContact = repository.contactCreate(authToken, newContact.getName());
		System.out.println("\n ############################# CONTACTO GUARDADO con id="+persistentContact.getID());
		
		persistentContact.setEmail(newContact.getEmail());
		persistentContact.setPhoneNumber(newContact.getPhoneNumber());
		Contact mostUpdateContact = repository.contactUpdate(authToken, persistentContact);
		System.out.println("\n ############################# CONTACTO ACTUALIZADO con id="+mostUpdateContact.getID());
				
		return mostUpdateContact;
	}
}
